//
//  sbrushAppDelegate.m
//  sbrush
//
//  Created by Phillip Popp on 4/23/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "sbrush_app_delegate.h"
#import "sms_analysis_controller.h"
#import "build_controller.h"
#import "pantomime_controller.h"
#import "file_item.h"
#import "tablet_instrument_view.h"
#import "instrument_view.h"
#import "dot_view.h"
#import "sbrush_audio_manager.h"

using namespace sbrush;

@implementation sbrushAppDelegate

@synthesize smsItems;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
	// Insert code here to initialize your application 
}

- (id)init {
    self = [super init];
    if (self != nil) {
		/* main arrays of file trackers etc. */
		smsItems = [[NSMutableArray alloc] init];
		pantomimeItems = [[NSMutableArray alloc] init];
		modelItems = [[NSMutableArray alloc] init];
		
		/* tablet io, view, and instrument */
		tabletInstrumentView = [[TabletInstrumentView alloc] init];
		instrument = [tabletInstrumentView instrument];
		instrumentView = [[DotView alloc] init];
		[instrumentView setFrame:NSMakeRect(0, 0, 100, 100)];
		[tabletInstrumentView addSubview:instrumentView];
		
		/* main window controllers */
		smsAnalController = [[SmsAnalysisController alloc] initWithSmsArray:smsItems];
		pantomimeController = [[PantomimeController alloc] initWithSmsArray:smsItems
															 pantomimeArray:pantomimeItems
															  pantomimeView:tabletInstrumentView
															 instrumentView:instrumentView
																 instrument:instrument];
		buildController = [[BuildController alloc] initWithPantomimeArray:pantomimeItems
															   modelArray:modelItems
															   instrument:instrument
														   instrumentView:instrumentView];
		/* initialize audio */
		AudioManager::init();
		AudioManager::start(0, 2, 44100, 1024);
    }
    
    return self;
}

- (void)dealloc {
	/* TODO: find an apporpriate place to save all items */
	AudioManager::stop();
	[instrumentView stopDrawing];
	[tabletInstrumentView release];
	[instrumentView release];
    [smsAnalController release];
	[pantomimeController release];
	[buildController release];
	[smsItems release];
    [super dealloc];
}

- (IBAction)smsWindow:(id)sender {
	[[smsAnalController window] makeKeyAndOrderFront:sender];
}

- (IBAction)pantomimeWindow:(id)sender {
	[[pantomimeController window] makeKeyAndOrderFront:sender];
	[instrumentView startDrawing];
}

- (IBAction)buildWindow:(id)sender {
	[[buildController window] makeKeyAndOrderFront:sender];
}

- (void)cancelOperation:(id)sender {
	NSLog(@"receive cancel op");
}

@end
